package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.Date;

/**
 * 考勤汇总查询,任务汇总查询 界面
 */
public class AttendanceSummaryQueryFragment extends BaseFragment implements OnClickListener {
  protected static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.EXTRA_VALUE";
  private static final int REQUEST_ACQUIRE_STARTTIME = 0;
  private static final int REQUEST_ACQUIRE_FINISHTIME = 1;
  private static final int REQUEST_STAFF = 3;
  private static final int REQUEST_SING_TYPE = 4;

  protected static final String REQUEST_STAFF_NAME =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.REQUEST_STAFF_NAME";
  protected static final String REQUEST_BEGIN_DATE =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.REQUEST_BEGIN_DATE";
  protected static final String REQUEST_END_DATE =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.REQUEST_END_DATE";
  protected static final String REQUEST_JOB_NUMBER =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.REQUEST_JOB_NUMBER";
  protected static final String REQUEST_TYPE_NAME =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.REQUEST_TYPE_NAME";
  protected static final String REQUEST_TYPE_CODE =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.REQUEST_TYPE_CODE";

  //这个查询界面很多地方使用，根据VALUE_FROM判断来自考勤打卡汇总还是任务汇总
  protected static final String VALUE_FROM =
      "com.isunland.managesystem.ui.AttendanceSummaryQueryFragment.VALUE_FROM";
  protected static final String TASK_COLLECT = "TASK_COLLECT";//任务汇总
  protected static final String ATTENT_SUMMARY = "ATTENT_SUMMARY";//打卡汇总
  protected static final String SIGN_SUMMARY = "SIGN_SUMMARY";//考勤汇总

  private TextView mtv_staffName_workConvert;// 员工
  private TextView mtv_startTime_workConvert;// 开始时间
  private TextView mtv_endTime_workConvert;// 结束时间
  private TextView mtv_signType_workConvert;// 打卡类型
  private String mBeginTime;
  private String mEndTime;
  private String mjobNos;
  private String mStaffName;
  private String mTypeName;
  private String mTypeCode;
  private String from;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.sift);
    mjobNos = getArguments().getString(REQUEST_JOB_NUMBER);
    mStaffName = getArguments().getString(REQUEST_STAFF_NAME);
    mBeginTime = getArguments().getString(REQUEST_BEGIN_DATE);
    mEndTime = getArguments().getString(REQUEST_END_DATE);
    mTypeCode = getArguments().getString(REQUEST_TYPE_CODE);
    mTypeName = getArguments().getString(REQUEST_TYPE_NAME);
    from = getArguments().getString(VALUE_FROM);
    LogUtil.i("mjobNos==" + mjobNos);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_attendancesummary_time, container, false);
    TextView mtv_staffName_title = (TextView) view.findViewById(R.id.tv_staffName_title);
    mtv_startTime_workConvert = (TextView) view.findViewById(R.id.tv_startTime_workConvert);
    mtv_endTime_workConvert = (TextView) view.findViewById(R.id.tv_endTime_workConvert);
    mtv_staffName_workConvert = (TextView) view.findViewById(R.id.tv_staffName_workConvert);
    mtv_signType_workConvert = (TextView) view.findViewById(R.id.tv_signType_workConvert);
    LinearLayout signType = (LinearLayout) view.findViewById(R.id.ll_sign_type);
    if (from != null && from.equals(TASK_COLLECT)) {
      signType.setVisibility(View.GONE);
    } else {
      mtv_staffName_title.setText(R.string.depetOrPerson);
    }

    mtv_startTime_workConvert.setText(mBeginTime);
    mtv_endTime_workConvert.setText(mEndTime);
    mtv_staffName_workConvert.setText(mStaffName);
    mtv_signType_workConvert.setText(mTypeName);

    mtv_startTime_workConvert.setOnClickListener(this);
    mtv_endTime_workConvert.setOnClickListener(this);
    mtv_staffName_workConvert.setOnClickListener(this);
    mtv_signType_workConvert.setOnClickListener(this);
    return view;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 开始时间
      case R.id.tv_startTime_workConvert:
        showDialog(REQUEST_ACQUIRE_STARTTIME,
            MyDateUtil.stringToDate(mBeginTime, MyDateUtil.TYPE_DATE));
        break;
      // 结束时间
      case R.id.tv_endTime_workConvert:
        showDialog(REQUEST_ACQUIRE_FINISHTIME,
            MyDateUtil.stringToDate(mEndTime, MyDateUtil.TYPE_DATE));
        break;
      // 获取员工列表
      case R.id.tv_staffName_workConvert:
        //                Intent i = new Intent(getActivity(), SearchPersonMultipleActicity.class);
        //                String from = getArguments().getString(SearchPersonMultipleFragment.EXTRA_FROM);
        //                if (from != null && from.equals(TASK_COLLECT)) {
        //                    i.putExtra(SearchPersonMultipleFragment.EXTRA_FROM, SearchPersonMultipleFragment.VALUE_TASK_COLLECT);
        //                }
        //                i.putExtra(SearchPersonMultipleFragment.EXTRA_JOB, mjobNos);
        //                startActivityForResult(i, REQUEST_STAFF);

        if (from != null && from.equals(TASK_COLLECT)) {
          //任务汇总：人员列表
          Intent i = new Intent(getActivity(), SearchPersonMultipleActicity.class);
          i.putExtra(SearchPersonMultipleFragment.EXTRA_FROM,
              SearchPersonMultipleFragment.VALUE_TASK_COLLECT);
          i.putExtra(SearchPersonMultipleFragment.EXTRA_JOB, mjobNos);
          startActivityForResult(i, REQUEST_STAFF);
        } else if (ATTENT_SUMMARY.equalsIgnoreCase(from) || SIGN_SUMMARY.equalsIgnoreCase(from)) {
          //考勤打卡汇总：人员和部门列表
          Intent i = new Intent(getActivity(), PDeptMultiListActivity.class);
          i.putExtra(REQUEST_JOB_NUMBER, mjobNos);
          startActivityForResult(i, REQUEST_STAFF);
        }
        break;

      case R.id.tv_signType_workConvert:
        showDialog(REQUEST_SING_TYPE, null);
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_ACQUIRE_STARTTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;
      case REQUEST_ACQUIRE_FINISHTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;

      case REQUEST_SING_TYPE:
        if (ATTENT_SUMMARY.equalsIgnoreCase(from)) {
          //打卡汇总打卡范围
          dialog = new AttendanceTypeDialogFragment();
        } else if (SIGN_SUMMARY.equalsIgnoreCase(from)) {
          //考勤汇总打卡范围
          dialog = new SignTypeDialogFragment();
        }

        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (dialog != null) {
      dialog.setTargetFragment(AttendanceSummaryQueryFragment.this, requestCode);
      dialog.show(fm, "");
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮
      case android.R.id.home:
        getActivity().finish();
        break;

      case R.id.menu_item_confirm:
        LogUtil.i("mjobNos==" + mjobNos);
        if (TextUtils.isEmpty(mStaffName)) {
          ToastUtil.show(MyStringUtil.connect(R.string.pleaseSelect, R.string.employee));
          return false;
        }
        Intent intent = new Intent();
        intent.putExtra(REQUEST_BEGIN_DATE, mBeginTime);
        intent.putExtra(REQUEST_END_DATE, mEndTime);
        intent.putExtra(REQUEST_JOB_NUMBER, mjobNos);
        intent.putExtra(REQUEST_STAFF_NAME, mStaffName);
        intent.putExtra(REQUEST_TYPE_CODE, mTypeCode);
        intent.putExtra(REQUEST_TYPE_NAME, mTypeName);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_ACQUIRE_STARTTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      //noinspection deprecation
      mBeginTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mtv_startTime_workConvert.setText(mBeginTime);
    }

    if (requestCode == REQUEST_ACQUIRE_FINISHTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      //noinspection deprecation
      mEndTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      getActivity().setResult(REQUEST_ACQUIRE_FINISHTIME);
      mtv_endTime_workConvert.setText(mEndTime);
      return;
    }

    if (requestCode == REQUEST_STAFF && data != null) {

      mStaffName = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_NAME);
      mjobNos = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_JOB);
      LogUtil.i("mjobNos===---===---" + mjobNos);
      mtv_staffName_workConvert.setText(mStaffName);
    }

    if (requestCode == REQUEST_SING_TYPE && data != null) {
      CustomerDialog content = (CustomerDialog) data.getSerializableExtra(EXTRA_VALUE);
      mTypeCode = content.getId();
      mTypeName = content.getName();
      mtv_signType_workConvert.setText(mTypeName);
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  public static Fragment newInstance(String name, String start, String end, String from,
      String typeName, String typeCode, String jobNos) {
    Bundle bundle = new Bundle();
    bundle.putString(REQUEST_STAFF_NAME, name);
    bundle.putString(REQUEST_BEGIN_DATE, start);
    bundle.putString(REQUEST_END_DATE, end);
    bundle.putString(REQUEST_TYPE_CODE, typeCode);
    bundle.putString(REQUEST_TYPE_NAME, typeName);
    bundle.putString(VALUE_FROM, from);
    bundle.putString(REQUEST_JOB_NUMBER, jobNos);
    AttendanceSummaryQueryFragment fragment = new AttendanceSummaryQueryFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
